package com.jsbb.controller;

import com.alibaba.fastjson.JSON;
import com.jsbb.annotation.ApiLog;
import com.jsbb.common.Message;
import com.jsbb.common.UserToken;
import com.jsbb.constant.BizConstant;
import com.jsbb.dto.LoginDto;
import com.jsbb.model.sys.SysUser;
import com.jsbb.service.sys.SysMenuService;
import com.jsbb.service.sys.SysUserService;
import com.jsbb.util.CookieUtils;
import com.jsbb.util.JwtUtils;
import com.jsbb.util.RedisUtils;
import com.jsbb.vo.LoginVo;
import com.jsbb.vo.sys.SysMenuVo;
import com.jsbb.vo.sys.SysUserDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录Controller
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
@RestController
@Api(tags = {"1.1 登录模块"})
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiLog("登录")
    public Message<LoginVo> login(@Validated @RequestBody LoginDto loginDto, HttpServletRequest request, HttpServletResponse response) {
        SysUser userDto = sysUserService.selectByLoginName(loginDto.getLoginName());
        if (userDto == null) {
            return Message.tips("用户名不存在");
        }
        if (!BCrypt.checkpw(loginDto.getPwd(), userDto.getPwd())) {
            return Message.tips("密码错误");
        }
        LoginVo loginVo = new LoginVo();
        //设置用户信息
        SysUserDetailVo userDetail = new SysUserDetailVo();
        BeanUtils.copyProperties(userDto, userDetail);
        loginVo.setSysUser(userDetail);
        //获取用户权限
        List<SysMenuVo> menuVoList = sysMenuService.selectByUserId(userDto.getId());
        loginVo.setSysMenu(menuVoList);
        //生成token
        UserToken userToken = new UserToken();
        userToken.setId(userDto.getId());
        userToken.setTimestamp(System.currentTimeMillis());
        loginVo.setToken(JwtUtils.build(JSON.toJSONString(userToken)));
        CookieUtils.setCookie(response, BizConstant.COOKIE_NAME, loginVo.getToken());
        //用户信息写入redis
        RedisUtils.set(BizConstant.USER_TOKEN + loginVo.getToken(), userToken, 1800);
        return Message.success(loginVo);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出")
    @ApiLog("退出")
    public Message logout(HttpServletRequest request, HttpServletResponse response) {
        RedisUtils.delete(BizConstant.USER_TOKEN + request.getHeader("token"));
        CookieUtils.deleteCookie(response, BizConstant.COOKIE_NAME);
        return Message.success();
    }
}
